﻿using System;
using System.Collections.Generic;
using System.IO;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Yule8xCommon.Applib
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>参考：
    /// <para>http://fanrsh.cnblogs.com/archive/2006/06/08/420546.html</para>
    /// <para>http://www.cnblogs.com/wangsaiming/archive/2013/01/11/2856253.html</para>
    /// </remarks>
    public class Log4NetUtil:ILogUtil
    {

        log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");

        Action<string, Exception> outputHandler = null;

        public void WriteInfo(string message)
        {
            if (loginfo.IsInfoEnabled)
            {
                if (null != outputHandler)
                {
                    outputHandler(message, null);
                }else
                {
                    loginfo.Info(message);
                }
            }
        }

        public void WriteError(string message, Exception se)
        {
            if (logerror.IsErrorEnabled)
            {
                if (null != outputHandler)
                {
                    outputHandler(message, se);
                }
                else
                {
                    logerror.Error(message, se);
                }
            }
        }

        public void SetOutputHandler(Action<string, Exception> action)
        {
            outputHandler = action;
        }
    }
}